﻿using HighPerformanceLog.V0;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HighPerformanceLog.V1
{
    public class Functions
    {
        /// <summary>
        /// 初始化日志，包括函数名，方法名，行号，文件名等信息。
        /// </summary>
        /// <param name="content">日志内容。</param>
        /// <param name="logLevel">日志类型。</param>
        /// <param name="traceLevel">追踪等级，默认为1。每额外封装一层则加1。</param>
        public static void TrackInfo(int traceLevel = 1)
        {
            StackFrame st = new StackTrace(traceLevel, true).GetFrame(0);
            string Filepath = st?.GetFileName() ?? "";
            string ClassName = st?.GetMethod()?.DeclaringType?.FullName ?? "";
            string MethodName = st?.GetMethod()?.Name ?? "";

            int LineNumber = st?.GetFileLineNumber() ?? 0;
            string Path = $"{ClassName}.{MethodName}@L{LineNumber}";
        }

        public static void AttachStackFrameToLog(HWLog log, StackFrame st)
        {
            if (st != null)
            {
                log.Method = st.GetMethod().DeclaringType.FullName + "." + st.GetMethod().Name;
                log.Position = st.GetFileName() + "@" + st.GetFileLineNumber();
            }
        }

        public static void AttachStackFrameToLog(HWLog log)
        {
            var st = GetStackFrame(2);
            if (st != null)
            {
                log.Method = st.GetMethod().DeclaringType.FullName + "." + st.GetMethod().Name;
                log.Position = st.GetFileName() + "@" + st.GetFileLineNumber();
            }
        }

        public static StackFrame GetStackFrame(int traceLevel = 1)
        {
            return new StackTrace(traceLevel, true).GetFrame(0);
        }

    }
}
